﻿/*
Medion Info Display

Copyright 2010 Wolf Posdorfer,
all portions of this codebase are copyrighted to the people
listed in contributors.txt.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//using System.Runtime.InteropServices;
using MD8800.util;

namespace MD8800
{
    public partial class Form1 : Form
    {
        private PortController _port;

        private bool _show_time;
        private int _currentminute;
        private cpu _cpu;

        public Form1()
        {
            InitializeComponent();
            _port = new PortController();
            _show_time = true;
            _cpu = new cpu();
            listBox1.SelectedIndex = 0;
            button1_Click(null, null);
        }

        #region /*GUI ELEMENTE*/
        private void button1_Click(object sender, EventArgs e)
        {
            //connect

            if (_port.isOpen())
            {
                label1.Text = "port wurde geschlossen";
                _port.clearScreen(true);
                _port.closePort();
            }
            else
            {
                if (_port.openport())
                {
                    label1.Text = "Port offen";
                    _port.write(opcodes.SET_TIME + Time.gibZeit());
                    _currentminute = Time.getMinute();
                    _port.drawborder(4, 3);
                    _port.write(opcodes.VOLUME_RED_BORDER + opcodes.BRIGHT_3);
                    volume.setVolume(_port);
                    timer1.Enabled = true;
                }
                else
                    label1.Text = "fehler";
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            String s = richTextBox1.Text;

            label1.Text = "" + volume.getVolume();

            volume.setVolume(_port);


            //_port.write(s);
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            _port.clearScreen(true);
            _port.closePort();
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            if (FormWindowState.Minimized == this.WindowState)
            {
                notifyIcon1.Visible = true;
                notifyIcon1.ShowBalloonTip(500, "Minimiert", "Das Programm wurde Minimiert", ToolTipIcon.Info);
                this.Hide();
            }
            else if (FormWindowState.Normal == this.WindowState)
            {
                notifyIcon1.Visible = false;
            }
        }

        private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
        {

            this.Show();
            notifyIcon1.Visible = false;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            volume.setVolume(_port);

            if (listBox1.SelectedIndex == 0)
            {
                if (_show_time && _currentminute != Time.getMinute())
                {
                    _port.write(opcodes.SET_TIME + Time.gibZeit());
                }
            }
            else if (listBox1.SelectedIndex == 1)
            {
                // Show Time, but check for programs
            }
            else if (listBox1.SelectedIndex == 2)
            {
                _cpu.writeCPUandRam(_port);
            }
        }
        #endregion /*GUI ELEMENTE*/
    }
}
